package com.genuine.jubao;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitMapCompress {
	private static int maxLength = 800;
	
	public static Bitmap exec(String picUrl) {
		try {
			//获取源图片的大小
			BitmapFactory.Options opts = new BitmapFactory.Options();
			//当opts不为null时，但decodeFile返回空，不为图片分配内存，只获取图片的大小，
			//并保存在opts的outWidth和outHeight
			BitmapFactory.decodeFile("/sdcard/Stephen.jpg", opts);
			int srcWidth = opts.outWidth;
			int srcHeight = opts.outHeight;
			int destWidth = 0;
			int destHeight = 0;
			//缩放的比�?			
			double ratio = 0.0;
			//按比例计算缩放后的图片大小，maxLength是长或宽允许的最大长�?			
			if(srcWidth >srcHeight) {
				ratio = srcWidth / maxLength;
				destWidth = maxLength;
				destHeight = (int) (srcHeight / ratio);
			}
			else {
				ratio = srcHeight / maxLength;
				destHeight = maxLength;
				destWidth = (int) (srcWidth / ratio); 
			}
			
			//对图片进行压缩，是在读取的过程中进行压缩，�?
			//不是把图片读进了内存再进行压�?		
			BitmapFactory.Options newOpts = new BitmapFactory.Options();
			//缩放的比例，缩放是很难按准备的比例进行缩放的，目前我只发现只能�?
			//过inSampleSize来进行缩放，其�?表明缩放的�?数，SDK中建议其值是2的指数�?
			newOpts.inSampleSize = (int) ratio + 1;
			//inJustDecodeBounds设为false表示把图片读进内存中
			newOpts.inJustDecodeBounds = false;
			//设置大小，这个一般是不准确的，是以inSampleSize的为准，
			//但是如果不设置却不能缩放
			newOpts.outHeight = destHeight;
			newOpts.outWidth = destWidth;
			//获取缩放后图�?			
			Bitmap destBm = BitmapFactory.decodeFile(picUrl, newOpts);
			
			if(destBm != null) {
				return destBm;
			} 
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
